﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class BF_RoomWork : UserControl
    {
        public BF_RoomWork()
        {
            InitializeComponent();

            this.pnlTop.Paint += new PaintEventHandler(pnlTop_Paint);
            this.pnlTop.Click += new EventHandler(pnlTop_Click);
            this.tlpTop.Click += new EventHandler(pnlTop_Click);
            this.lblNumber.Click += new EventHandler(pnlTop_Click);
            this.lblStats.Click += new EventHandler(pnlTop_Click);
            this.lblType.Click += new EventHandler(pnlTop_Click);

            this.LostFocus += new EventHandler(BF_RoomWork_LostFocus);
            this.GotFocus += new EventHandler(BF_RoomWork_GotFocus);
        }


        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }


        #region - 自定义属性 -
        
        private string[] t_Itme;
        private Color t_BorderColor = Color.Silver;
        private Status t_RoomStatus;
        private Color t_StatusColor = Color.FromArgb(44, 202, 55);
        private bool t_DownShow = true;
        private string t_Type;
        private string t_Number;
        private string t_Id;
        private bool t_Success;
        private bool t_Raipr;
        private string t_FixNumber = "1";

        [Category("Ternence"), Description("操作项")]
        public string[] T_Itme
        {
            get { return t_Itme; }
            set
            {
                t_Itme = value;
                if (t_Itme.Length > 0)
                {
                    for (int i = 0; i < t_Itme.Length; i++)
                    {
                        Button btn = new Button();
                        if (t_Itme[i].Length == 2)
                            btn.Width = 50;
                        if (t_Itme[i].Length == 4)
                            btn.Width = 70;
                        btn.Height = 38;
                        btn.Text = t_Itme[i];
                        btn.Tag = i + 1;
                        btn.FlatStyle = FlatStyle.Flat;
                        btn.ForeColor = Color.FromArgb(67, 187, 188);
                        btn.FlatAppearance.BorderSize = 1;
                        btn.FlatAppearance.MouseDownBackColor = Color.FromArgb(67, 167, 170);
                        btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(67, 187, 188);
                        btn.FlatAppearance.BorderColor = Color.Orange;
                        btn.Font = new System.Drawing.Font(@"微软雅黑", 8);
                        btn.Margin = new Padding(0, 0, 1, 0);
                        btn.Paint += new PaintEventHandler(btn_Paint);
                        btn.Click += new EventHandler(btn_Click);
                        btn.GotFocus += new EventHandler(btn_GotFocus);
                        btn.MouseEnter += new EventHandler(btn_MouseEnter);
                        btn.MouseLeave += new EventHandler(btn_MouseLeave);
                        this.flpDow.Controls.Add(btn);
                    }
                }
            }
        }

        [Category("Ternence"), Description("边框颜色")]
        public Color T_BorderColor
        {
            get { return t_BorderColor; }
            set { t_BorderColor = value; }
        }

        [Category("Ternence"), Description("是否显示操作栏")]
        public bool T_DownShow
        {
            get { return t_DownShow; }
            set
            {
                t_DownShow = value;
                foreach (Control item in this.flpDow.Controls)
                {
                    item.Visible = value;
                }
            }
        }

        [Category("Ternence"), Description("包间状态")]
        public Status T_RoomStatus
        {
            get { return t_RoomStatus; }
            set
            {
                if (t_RoomStatus != value)
                {
                    t_RoomStatus = value;
                    if (t_RoomStatus == Status.空闲中)
                        t_StatusColor = Color.FromArgb(44, 202, 55);
                    if (t_RoomStatus == Status.占用中)
                        t_StatusColor = Color.Silver;
                    if (t_RoomStatus == Status.维修中)
                        t_StatusColor = Color.Red;
                    if (t_RoomStatus == Status.预定中)
                        t_StatusColor = Color.FromArgb(246, 195, 18);
                    lblStats.Text = t_RoomStatus.ToString();
                    lblStats.ForeColor = t_StatusColor;
                    PnlPaint();
                }
            }
        }

        [Category("Ternence"), Description("包间类型")]
        public string T_Type
        {
            get
            {
                t_Type = lblType.Text.Trim();
                return t_Type;
            }
            set
            {
                t_Type = value;
                lblType.Text = value;
            }
        }

        [Category("Ternence"), Description("包间编号")]
        public string T_Number
        {
            get
            {
                t_Number = lblNumber.Text;
                return t_Number;
            }
            set
            {
                t_Number = value;
                lblNumber.Text = value;
            }
        }

        [Category("Ternence"), Description("包间ID")]
        public string T_Id
        {
            get { return t_Id; }
            set { t_Id = value; }
        }

        [Category("Ternence"), Description("上/下座是否成功")]
        public bool T_Success
        {
            get { return t_Success; }
            set
            {
                t_Success = value;
                if (t_Success)
                {
                    foreach (Control item in flpDow.Controls)
                    {
                        if (item.Tag.ToString() == "1")
                        {
                            item.Tag = "5";
                            item.Text = "下座";
                            break;
                        }
                        if (item.Tag.ToString() == "5")
                        {
                            item.Tag = "1";
                            item.Text = "上座";
                            break;
                        }
                    }
                }
            }
        }

        [Category("Ternence"), Description("设置/取消维修是否成功")]
        public bool T_Raipr
        {
            get {return t_Raipr; }
            set 
            { 
                t_Raipr = value;
                if (t_Raipr)
                {
                    foreach (Control item in flpDow.Controls)
                    {
                        if (item.Tag.ToString() == "3")
                        {
                            item.Tag = "6";
                            item.Text = "恢复";
                            break;
                        }
                        if (item.Tag.ToString() == "6")
                        {
                            item.Tag = "3";
                            item.Text = "维护";
                            break;
                        }
                    }
                }
            }
        }

        [Category("Ternence"),Description("当前预订人数")]
        public string T_FixNumber
        {
            get 
            {
                t_FixNumber = lblFix.Text;
                return t_FixNumber;
            }
            set 
            { 
                t_FixNumber = value;
                lblFix.Text = value;
                if (t_FixNumber == "0")
                {
                    lblPrice.Text = "";
                    lblFix.Visible = false;
                }
                else
                {
                    lblPrice.Text = "人已预定";
                    lblFix.Visible = true;
                }
            }
        }

        #endregion


        /// <summary>
        /// 绘制控件
        /// </summary>
        void pnlTop_Paint(object sender, PaintEventArgs e)
        {
            PnlPaint();
        }

        private void PnlPaint()
        {
            using (Pen p = new Pen(T_BorderColor, 10))
            {
                int with = this.Width / 4;
                Graphics gs = pnlTop.CreateGraphics();//.Graphics;
                gs.DrawLine(p, 0, 0, this.Width, 0);
                gs.DrawLine(p, 0, 0, 0, this.Height - this.flpDow.Height);
                gs.DrawLine(p, this.Width - 1, 0, this.Width - 1, this.Height - this.flpDow.Height);

                gs.DrawLine(p, 0, this.Height - this.flpDow.Height, this.Width - with * 3, this.Height - this.flpDow.Height);
                gs.DrawLine(p, this.Width - with, this.Height - this.flpDow.Height, this.Width - 1, this.Height - this.flpDow.Height);
                Pen pe = new Pen(t_StatusColor, 20);
                gs.DrawLine(pe, this.Width - with * 3 + 10, this.Height - this.flpDow.Height, this.Width - with - 10, this.Height - this.flpDow.Height);
            }
        }


        /// <summary>
        /// 隐藏操作按钮
        /// </summary>
        void pnlTop_Click(object sender, EventArgs e)
        {
            this.Focus();
        }


        /// <summary>
        /// 获取焦点
        /// </summary>
        void BF_RoomWork_GotFocus(object sender, EventArgs e)
        {
            for (int i = 0; i < this.flpDow.Controls.Count; i++)
                this.flpDow.Controls[i].Visible = true;
        }

        /// <summary>
        /// 失去焦点
        /// </summary>
        void BF_RoomWork_LostFocus(object sender, EventArgs e)
        {
            for (int i = 0; i < this.flpDow.Controls.Count; i++)
            {
                if (this.flpDow.Controls[i].Focused)
                    return;
            }
            for (int i = 0; i < this.flpDow.Controls.Count; i++)
                this.flpDow.Controls[i].Visible = false;
        }


        public delegate void ButtonClickEventHanld(object sender, int index);
        [Category("Ternence"), Description("按钮操作事件")]
        public event ButtonClickEventHanld ButtonClickEvent;
        /// <summary>
        /// 按钮单击事件
        /// </summary>
        void btn_Click(object sender, EventArgs e)
        {
            if (ButtonClickEvent != null)
            {
                Button btn = sender as Button;
                int index = 0;
                int.TryParse(btn.Tag.ToString(), out index);
                ButtonClickEvent(this, index);
            }
        }

        public delegate void FixNumberClickEvenHanld(object sender, EventArgs e);
        [Category("Ternence"),Description("预订人数事件")]
        public event FixNumberClickEvenHanld FixNumberClickEven;
        private void lblFix_Click(object sender, EventArgs e)
        {
            if (FixNumberClickEven != null)
                FixNumberClickEven(this, e);
        }

        #region - 控件样式 -

        /// <summary>
        /// 按钮边框
        /// </summary>
        void btn_Paint(object sender, PaintEventArgs e)
        {
            Button btn = sender as Button;
            using (Pen p = new Pen(Color.Silver))
            {
                Graphics gs = e.Graphics;
                gs.DrawRectangle(p, 0, 0, btn.Width - 1, btn.Height - 1);
            }

        }


        /// <summary>
        /// 操作按钮获取焦点
        /// </summary>
        void btn_GotFocus(object sender, EventArgs e)
        {
            this.Focus();
        }

        /// <summary>
        /// 鼠标进入区域
        /// </summary>
        void btn_MouseEnter(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.ForeColor = Color.White;
        }

        /// <summary>
        /// 鼠标离开区域
        /// </summary>
        void btn_MouseLeave(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.ForeColor = Color.FromArgb(67, 187, 188);
            btn.BackColor = Color.White;
        }

        #endregion

        

        
    }

    public enum Status
    {
        /// <summary>
        /// 空闲
        /// </summary>
        空闲中,
        /// <summary>
        /// 预定
        /// </summary>
        预定中,
        /// <summary>
        /// 占用
        /// </summary>
        占用中,
        /// <summary>
        /// 维修
        /// </summary>
        维修中
    }

}
